﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;

namespace FsAddInManager
{
    public class SplitButton : Button
    {
        public SplitButton()
        {
            this.AutoSize = true;
        }

        [Browsable(false)]
        public override ContextMenuStrip ContextMenuStrip
        {
            get { return this.SplitMenuStrip; }
            set { this.SplitMenuStrip = value; }
        }

        [DefaultValue(null)]
        public ContextMenu SplitMenu
        {
            get { return this.m_SplitMenu; }
            set
            {
                if (this.m_SplitMenu != null)
                {
                    this.m_SplitMenu.Popup -= this.SplitMenu_Popup;
                }

                if (value != null)
                {
                    this.ShowSplit = true;
                    value.Popup += this.SplitMenu_Popup;
                }
                else
                {
                    this.ShowSplit = false;
                }

                this.m_SplitMenu = value;
            }
        }

        [DefaultValue(null)]
        public ContextMenuStrip SplitMenuStrip
        {
            get { return this.m_SplitMenuStrip; }
            set
            {
                if (this.m_SplitMenuStrip != null)
                {
                    this.m_SplitMenuStrip.Closing -= this.SplitMenuStrip_Closing;
                    this.m_SplitMenuStrip.Opening -= this.SplitMenuStrip_Opening;
                }

                if (value != null)
                {
                    this.ShowSplit = true;
                    value.Closing += this.SplitMenuStrip_Closing;
                    value.Opening += this.SplitMenuStrip_Opening;
                }
                else
                {
                    this.ShowSplit = false;
                }

                this.m_SplitMenuStrip = value;
            }
        }

        [DefaultValue(false)]
        public bool ShowSplit
        {
            set
            {
                if (value != this.showSplit)
                {
                    this.showSplit = value;
                    base.Invalidate();
                    if (base.Parent != null)
                    {
                        base.Parent.PerformLayout();
                    }
                }
            }
        }

        private PushButtonState State
        {
            get { return this._state; }
            set
            {
                if (!this._state.Equals(value))
                {
                    this._state = value;
                    base.Invalidate();
                }
            }
        }

        protected override bool IsInputKey(Keys keyData)
        {
            return (keyData.Equals(Keys.Down) && this.showSplit) || base.IsInputKey(keyData);
        }

        protected override void OnGotFocus(EventArgs e)
        {
            if (!this.showSplit)
            {
                base.OnGotFocus(e);
                return;
            }

            if (!this.State.Equals(PushButtonState.Pressed) && !this.State.Equals(PushButtonState.Disabled))
            {
                this.State = PushButtonState.Default;
            }
        }

        protected override void OnKeyDown(KeyEventArgs kevent)
        {
            if (this.showSplit)
            {
                if (kevent.KeyCode.Equals(Keys.Down) && !this.isSplitMenuVisible)
                {
                    this.ShowContextMenuStrip();
                }
                else if (kevent.KeyCode.Equals(Keys.Space) && kevent.Modifiers == Keys.None)
                {
                    this.State = PushButtonState.Pressed;
                }
            }

            base.OnKeyDown(kevent);
        }

        protected override void OnKeyUp(KeyEventArgs kevent)
        {
            if (kevent.KeyCode.Equals(Keys.Space))
            {
                if (Control.MouseButtons == MouseButtons.None)
                {
                    this.State = PushButtonState.Normal;
                }
            }
            else if (kevent.KeyCode.Equals(Keys.Apps) && Control.MouseButtons == MouseButtons.None && !this.isSplitMenuVisible)
            {
                this.ShowContextMenuStrip();
            }

            base.OnKeyUp(kevent);
        }

        protected override void OnEnabledChanged(EventArgs e)
        {
            this.State = (base.Enabled ? PushButtonState.Normal : PushButtonState.Disabled);
            base.OnEnabledChanged(e);
        }

        protected override void OnLostFocus(EventArgs e)
        {
            if (!this.showSplit)
            {
                base.OnLostFocus(e);
                return;
            }

            if (!this.State.Equals(PushButtonState.Pressed) && !this.State.Equals(PushButtonState.Disabled))
            {
                this.State = PushButtonState.Normal;
            }
        }

        protected override void OnMouseEnter(EventArgs e)
        {
            if (!this.showSplit)
            {
                base.OnMouseEnter(e);
                return;
            }

            this.isMouseEntered = true;
            if (!this.State.Equals(PushButtonState.Pressed) && !this.State.Equals(PushButtonState.Disabled))
            {
                this.State = PushButtonState.Hot;
            }
        }

        protected override void OnMouseLeave(EventArgs e)
        {
            if (!this.showSplit)
            {
                base.OnMouseLeave(e);
                return;
            }

            this.isMouseEntered = false;
            if (!this.State.Equals(PushButtonState.Pressed) && !this.State.Equals(PushButtonState.Disabled))
            {
                this.State = (this.Focused ? PushButtonState.Default : PushButtonState.Normal);
            }
        }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            if (!this.showSplit)
            {
                base.OnMouseDown(e);
                return;
            }

            if (this.m_SplitMenu != null && e.Button == MouseButtons.Left && !this.isMouseEntered)
            {
                this.skipNextOpen = true;
            }

            if (this.dropDownRectangle.Contains(e.Location) && !this.isSplitMenuVisible && e.Button == MouseButtons.Left)
            {
                this.ShowContextMenuStrip();
                return;
            }

            this.State = PushButtonState.Pressed;
        }

        protected override void OnMouseUp(MouseEventArgs mevent)
        {
            if (!this.showSplit)
            {
                base.OnMouseUp(mevent);
                return;
            }

            if (mevent.Button == MouseButtons.Right && base.ClientRectangle.Contains(mevent.Location) && !this.isSplitMenuVisible)
            {
                this.ShowContextMenuStrip();
                return;
            }

            if ((this.m_SplitMenuStrip == null && this.m_SplitMenu == null) || !this.isSplitMenuVisible)
            {
                this.SetButtonDrawState();
                if (base.ClientRectangle.Contains(mevent.Location) && !this.dropDownRectangle.Contains(mevent.Location))
                {
                    this.OnClick(new EventArgs());
                }
            }
        }

        protected override void OnPaint(PaintEventArgs pevent)
        {
            base.OnPaint(pevent);
            if (!this.showSplit)
            {
                return;
            }

            Graphics graphics = pevent.Graphics;
            Rectangle clientRectangle = base.ClientRectangle;
            if (this.State != PushButtonState.Pressed && base.IsDefault && !Application.RenderWithVisualStyles)
            {
                Rectangle bounds = clientRectangle;
                bounds.Inflate(-1, -1);
                ButtonRenderer.DrawButton(graphics, bounds, this.State);
                graphics.DrawRectangle(SystemPens.WindowFrame, 0, 0, clientRectangle.Width - 1, clientRectangle.Height - 1);
            }
            else
            {
                ButtonRenderer.DrawButton(graphics, clientRectangle, this.State);
            }

            this.dropDownRectangle = new Rectangle(clientRectangle.Right - 18, 0, 18, clientRectangle.Height);
            int borderSize = SplitButton.BorderSize;
            Rectangle rectangle = new Rectangle(borderSize - 1, borderSize - 1, clientRectangle.Width - this.dropDownRectangle.Width - borderSize, clientRectangle.Height - borderSize * 2 + 2);
            bool flag = this.State == PushButtonState.Hot || this.State == PushButtonState.Pressed || !Application.RenderWithVisualStyles;
            if (this.RightToLeft == RightToLeft.Yes)
            {
                this.dropDownRectangle.X = clientRectangle.Left + 1;
                rectangle.X = this.dropDownRectangle.Right;
                if (flag)
                {
                    graphics.DrawLine(SystemPens.ButtonShadow, clientRectangle.Left + 18, SplitButton.BorderSize, clientRectangle.Left + 18, clientRectangle.Bottom - SplitButton.BorderSize);
                    graphics.DrawLine(SystemPens.ButtonFace, clientRectangle.Left + 18 + 1, SplitButton.BorderSize, clientRectangle.Left + 18 + 1, clientRectangle.Bottom - SplitButton.BorderSize);
                }
            }
            else if (flag)
            {
                graphics.DrawLine(SystemPens.ButtonShadow, clientRectangle.Right - 18, SplitButton.BorderSize, clientRectangle.Right - 18, clientRectangle.Bottom - SplitButton.BorderSize);
                graphics.DrawLine(SystemPens.ButtonFace, clientRectangle.Right - 18 - 1, SplitButton.BorderSize, clientRectangle.Right - 18 - 1, clientRectangle.Bottom - SplitButton.BorderSize);
            }

            this.PaintArrow(graphics, this.dropDownRectangle);
            this.PaintTextandImage(graphics, new Rectangle(0, 0, base.ClientRectangle.Width - 18, base.ClientRectangle.Height));
            if (this.State != PushButtonState.Pressed && this.Focused && this.ShowFocusCues)
            {
                ControlPaint.DrawFocusRectangle(graphics, rectangle);
            }
        }

        private void PaintTextandImage(Graphics g, Rectangle bounds)
        {
            Rectangle rectangle;
            Rectangle rectangle2;
            this.CalculateButtonTextAndImageLayout(ref bounds, out rectangle, out rectangle2);
            if (base.Image != null)
            {
                if (base.Enabled)
                {
                    g.DrawImage(base.Image, rectangle2.X, rectangle2.Y, base.Image.Width, base.Image.Height);
                }
                else
                {
                    ControlPaint.DrawImageDisabled(g, base.Image, rectangle2.X, rectangle2.Y, this.BackColor);
                }
            }

            if (!base.UseMnemonic)
            {
                this.textFormatFlags |= System.Windows.Forms.TextFormatFlags.NoPrefix;
            }
            else if (!this.ShowKeyboardCues)
            {
                this.textFormatFlags |= System.Windows.Forms.TextFormatFlags.HidePrefix;
            }

            if (!string.IsNullOrEmpty(this.Text))
            {
                if (base.Enabled)
                {
                    TextRenderer.DrawText(g, this.Text, this.Font, rectangle, SystemColors.ControlText, this.textFormatFlags);
                    return;
                }

                ControlPaint.DrawStringDisabled(g, this.Text, this.Font, this.BackColor, rectangle, this.textFormatFlags);
            }
        }

        private void PaintArrow(Graphics g, Rectangle dropDownRect)
        {
            Point point = new Point(Convert.ToInt32(dropDownRect.Left + dropDownRect.Width / 2), Convert.ToInt32(dropDownRect.Top + dropDownRect.Height / 2));
            point.X += dropDownRect.Width % 2;
            Point[] points = new Point[]
            {
                new Point(point.X - 2, point.Y - 1),
                new Point(point.X + 3, point.Y - 1),
                new Point(point.X, point.Y + 2)
            };
            if (base.Enabled)
            {
                g.FillPolygon(SystemBrushes.ControlText, points);
                return;
            }

            g.FillPolygon(SystemBrushes.ButtonShadow, points);
        }

        public override Size GetPreferredSize(Size proposedSize)
        {
            Size preferredSize = base.GetPreferredSize(proposedSize);
            if (this.showSplit)
            {
                if (this.AutoSize)
                {
                    return this.CalculateButtonAutoSize();
                }

                if (!string.IsNullOrEmpty(this.Text) && TextRenderer.MeasureText(this.Text, this.Font).Width + 18 > preferredSize.Width)
                {
                    return preferredSize + new Size(18 + SplitButton.BorderSize * 2, 0);
                }
            }

            return preferredSize;
        }

        private Size CalculateButtonAutoSize()
        {
            Size empty = Size.Empty;
            Size size = TextRenderer.MeasureText(this.Text, this.Font);
            Size size2 = (base.Image == null) ? Size.Empty : base.Image.Size;
            if (this.Text.Length != 0)
            {
                size.Height += 4;
                size.Width += 4;
            }

            switch (base.TextImageRelation)
            {
                case TextImageRelation.Overlay:
                    empty.Height = Math.Max((this.Text.Length == 0) ? 0 : size.Height, size2.Height);
                    empty.Width = Math.Max(size.Width, size2.Width);
                    break;
                case TextImageRelation.ImageAboveText:
                case TextImageRelation.TextAboveImage:
                    empty.Height = size.Height + size2.Height;
                    empty.Width = Math.Max(size.Width, size2.Width);
                    break;
                case TextImageRelation.ImageBeforeText:
                case TextImageRelation.TextBeforeImage:
                    empty.Height = Math.Max(size.Height, size2.Height);
                    empty.Width = size.Width + size2.Width;
                    break;
            }

            empty.Height += base.Padding.Vertical + 6;
            empty.Width += base.Padding.Horizontal + 6;
            if (this.showSplit)
            {
                empty.Width += 18;
            }

            return empty;
        }

        private void CalculateButtonTextAndImageLayout(ref Rectangle content_rect, out Rectangle textRectangle, out Rectangle imageRectangle)
        {
            Size textSize = TextRenderer.MeasureText(this.Text, this.Font, content_rect.Size, this.textFormatFlags);
            Size imageSize = (base.Image == null) ? Size.Empty : base.Image.Size;
            textRectangle = Rectangle.Empty;
            imageRectangle = Rectangle.Empty;
            switch (base.TextImageRelation)
            {
                case TextImageRelation.Overlay:
                    textRectangle = SplitButton.OverlayObjectRect(ref content_rect, ref textSize, this.TextAlign);
                    if (this._state == PushButtonState.Pressed && !Application.RenderWithVisualStyles)
                    {
                        textRectangle.Offset(1, 1);
                    }

                    if (base.Image != null)
                    {
                        imageRectangle = SplitButton.OverlayObjectRect(ref content_rect, ref imageSize, base.ImageAlign);
                        return;
                    }

                    break;
                case TextImageRelation.ImageAboveText:
                    content_rect.Inflate(-4, -4);
                    this.LayoutTextAboveOrBelowImage(content_rect, false, textSize, imageSize, out textRectangle, out imageRectangle);
                    return;
                case TextImageRelation.TextAboveImage:
                    content_rect.Inflate(-4, -4);
                    this.LayoutTextAboveOrBelowImage(content_rect, true, textSize, imageSize, out textRectangle, out imageRectangle);
                    return;
                case (TextImageRelation) 3:
                case (TextImageRelation) 5:
                case (TextImageRelation) 6:
                case (TextImageRelation) 7:
                    break;
                case TextImageRelation.ImageBeforeText:
                    content_rect.Inflate(-4, -4);
                    this.LayoutTextBeforeOrAfterImage(content_rect, false, textSize, imageSize, out textRectangle, out imageRectangle);
                    return;
                case TextImageRelation.TextBeforeImage:
                    content_rect.Inflate(-4, -4);
                    this.LayoutTextBeforeOrAfterImage(content_rect, true, textSize, imageSize, out textRectangle, out imageRectangle);
                    break;
                default:
                    return;
            }
        }

        private static Rectangle OverlayObjectRect(ref Rectangle container, ref Size sizeOfObject, System.Drawing.ContentAlignment alignment)
        {
            int x;
            int y;
            if (alignment <= System.Drawing.ContentAlignment.MiddleCenter)
            {
                switch (alignment)
                {
                    case System.Drawing.ContentAlignment.TopLeft:
                        x = 4;
                        y = 4;
                        goto IL_15A;
                    case System.Drawing.ContentAlignment.TopCenter:
                        x = (container.Width - sizeOfObject.Width) / 2;
                        y = 4;
                        goto IL_15A;
                    case (System.Drawing.ContentAlignment) 3:
                        break;
                    case System.Drawing.ContentAlignment.TopRight:
                        x = container.Width - sizeOfObject.Width - 4;
                        y = 4;
                        goto IL_15A;
                    default:
                        if (alignment == System.Drawing.ContentAlignment.MiddleLeft)
                        {
                            x = 4;
                            y = (container.Height - sizeOfObject.Height) / 2;
                            goto IL_15A;
                        }

                        if (alignment == System.Drawing.ContentAlignment.MiddleCenter)
                        {
                            x = (container.Width - sizeOfObject.Width) / 2;
                            y = (container.Height - sizeOfObject.Height) / 2;
                            goto IL_15A;
                        }

                        break;
                }
            }
            else if (alignment <= System.Drawing.ContentAlignment.BottomLeft)
            {
                if (alignment == System.Drawing.ContentAlignment.MiddleRight)
                {
                    x = container.Width - sizeOfObject.Width - 4;
                    y = (container.Height - sizeOfObject.Height) / 2;
                    goto IL_15A;
                }

                if (alignment == System.Drawing.ContentAlignment.BottomLeft)
                {
                    x = 4;
                    y = container.Height - sizeOfObject.Height - 4;
                    goto IL_15A;
                }
            }
            else
            {
                if (alignment == System.Drawing.ContentAlignment.BottomCenter)
                {
                    x = (container.Width - sizeOfObject.Width) / 2;
                    y = container.Height - sizeOfObject.Height - 4;
                    goto IL_15A;
                }

                if (alignment == System.Drawing.ContentAlignment.BottomRight)
                {
                    x = container.Width - sizeOfObject.Width - 4;
                    y = container.Height - sizeOfObject.Height - 4;
                    goto IL_15A;
                }
            }

            x = 4;
            y = 4;
            IL_15A:
            return new Rectangle(x, y, sizeOfObject.Width, sizeOfObject.Height);
        }

        private void LayoutTextBeforeOrAfterImage(Rectangle totalArea, bool textFirst, Size textSize, Size imageSize, out Rectangle textRect, out Rectangle imageRect)
        {
            int num = 0;
            int num2 = textSize.Width + num + imageSize.Width;
            if (!textFirst)
            {
                num += 2;
            }

            if (num2 > totalArea.Width)
            {
                textSize.Width = totalArea.Width - num - imageSize.Width;
                num2 = totalArea.Width;
            }

            int num3 = totalArea.Width - num2;
            int num4 = 0;
            HorizontalAlignment horizontalAlignment = SplitButton.GetHorizontalAlignment(this.TextAlign);
            HorizontalAlignment horizontalAlignment2 = SplitButton.GetHorizontalAlignment(base.ImageAlign);
            if (horizontalAlignment2 == HorizontalAlignment.Left)
            {
                num4 = 0;
            }
            else if (horizontalAlignment2 == HorizontalAlignment.Right && horizontalAlignment == HorizontalAlignment.Right)
            {
                num4 = num3;
            }
            else if (horizontalAlignment2 == HorizontalAlignment.Center && (horizontalAlignment == HorizontalAlignment.Left || horizontalAlignment == HorizontalAlignment.Center))
            {
                num4 += num3 / 3;
            }
            else
            {
                num4 += 2 * (num3 / 3);
            }

            Rectangle rectangle;
            Rectangle rectangle2;
            if (textFirst)
            {
                rectangle = new Rectangle(totalArea.Left + num4, SplitButton.AlignInRectangle(totalArea, textSize, this.TextAlign).Top, textSize.Width, textSize.Height);
                rectangle2 = new Rectangle(rectangle.Right + num, SplitButton.AlignInRectangle(totalArea, imageSize, base.ImageAlign).Top, imageSize.Width, imageSize.Height);
            }
            else
            {
                rectangle2 = new Rectangle(totalArea.Left + num4, SplitButton.AlignInRectangle(totalArea, imageSize, base.ImageAlign).Top, imageSize.Width, imageSize.Height);
                rectangle = new Rectangle(rectangle2.Right + num, SplitButton.AlignInRectangle(totalArea, textSize, this.TextAlign).Top, textSize.Width, textSize.Height);
            }

            textRect = rectangle;
            imageRect = rectangle2;
        }

        private void LayoutTextAboveOrBelowImage(Rectangle totalArea, bool textFirst, Size textSize, Size imageSize, out Rectangle textRect, out Rectangle imageRect)
        {
            int num = 0;
            int num2 = textSize.Height + num + imageSize.Height;
            if (textFirst)
            {
                num += 2;
            }

            if (textSize.Width > totalArea.Width)
            {
                textSize.Width = totalArea.Width;
            }

            if (num2 > totalArea.Height && textFirst)
            {
                imageSize = Size.Empty;
                num2 = totalArea.Height;
            }

            int num3 = totalArea.Height - num2;
            int num4 = 0;
            VerticalAlignment verticalAlignment = SplitButton.GetVerticalAlignment(this.TextAlign);
            VerticalAlignment verticalAlignment2 = SplitButton.GetVerticalAlignment(base.ImageAlign);
            if (verticalAlignment2 == VerticalAlignment.Top)
            {
                num4 = 0;
            }
            else if (verticalAlignment2 == VerticalAlignment.Bottom && verticalAlignment == VerticalAlignment.Bottom)
            {
                num4 = num3;
            }
            else if (verticalAlignment2 == VerticalAlignment.Center && (verticalAlignment == VerticalAlignment.Top || verticalAlignment == VerticalAlignment.Center))
            {
                num4 += num3 / 3;
            }
            else
            {
                num4 += 2 * (num3 / 3);
            }

            Rectangle rectangle;
            Rectangle rectangle2;
            if (textFirst)
            {
                rectangle = new Rectangle(SplitButton.AlignInRectangle(totalArea, textSize, this.TextAlign).Left, totalArea.Top + num4, textSize.Width, textSize.Height);
                rectangle2 = new Rectangle(SplitButton.AlignInRectangle(totalArea, imageSize, base.ImageAlign).Left, rectangle.Bottom + num, imageSize.Width, imageSize.Height);
            }
            else
            {
                rectangle2 = new Rectangle(SplitButton.AlignInRectangle(totalArea, imageSize, base.ImageAlign).Left, totalArea.Top + num4, imageSize.Width, imageSize.Height);
                rectangle = new Rectangle(SplitButton.AlignInRectangle(totalArea, textSize, this.TextAlign).Left, rectangle2.Bottom + num, textSize.Width, textSize.Height);
                if (rectangle.Bottom > totalArea.Bottom)
                {
                    rectangle.Y = totalArea.Top;
                }
            }

            textRect = rectangle;
            imageRect = rectangle2;
        }

        private static HorizontalAlignment GetHorizontalAlignment(System.Drawing.ContentAlignment align)
        {
            if (align <= System.Drawing.ContentAlignment.MiddleCenter)
            {
                switch (align)
                {
                    case System.Drawing.ContentAlignment.TopLeft:
                        break;
                    case System.Drawing.ContentAlignment.TopCenter:
                        return HorizontalAlignment.Center;
                    case (System.Drawing.ContentAlignment) 3:
                        return HorizontalAlignment.Left;
                    case System.Drawing.ContentAlignment.TopRight:
                        return HorizontalAlignment.Right;
                    default:
                        if (align != System.Drawing.ContentAlignment.MiddleLeft)
                        {
                            if (align != System.Drawing.ContentAlignment.MiddleCenter)
                            {
                                return HorizontalAlignment.Left;
                            }

                            return HorizontalAlignment.Center;
                        }

                        break;
                }
            }
            else if (align <= System.Drawing.ContentAlignment.BottomLeft)
            {
                if (align == System.Drawing.ContentAlignment.MiddleRight)
                {
                    return HorizontalAlignment.Right;
                }

                if (align != System.Drawing.ContentAlignment.BottomLeft)
                {
                    return HorizontalAlignment.Left;
                }
            }
            else
            {
                if (align == System.Drawing.ContentAlignment.BottomCenter)
                {
                    return HorizontalAlignment.Center;
                }

                if (align != System.Drawing.ContentAlignment.BottomRight)
                {
                    return HorizontalAlignment.Left;
                }

                return HorizontalAlignment.Right;
            }

            return HorizontalAlignment.Left;
        }

        private static VerticalAlignment GetVerticalAlignment(System.Drawing.ContentAlignment align)
        {
            if (align > System.Drawing.ContentAlignment.MiddleCenter)
            {
                if (align <= System.Drawing.ContentAlignment.BottomLeft)
                {
                    if (align == System.Drawing.ContentAlignment.MiddleRight)
                    {
                        return VerticalAlignment.Center;
                    }

                    if (align != System.Drawing.ContentAlignment.BottomLeft)
                    {
                        return VerticalAlignment.Top;
                    }
                }
                else if (align != System.Drawing.ContentAlignment.BottomCenter && align != System.Drawing.ContentAlignment.BottomRight)
                {
                    return VerticalAlignment.Top;
                }

                return VerticalAlignment.Bottom;
            }

            switch (align)
            {
                case System.Drawing.ContentAlignment.TopLeft:
                case System.Drawing.ContentAlignment.TopCenter:
                case System.Drawing.ContentAlignment.TopRight:
                    return VerticalAlignment.Top;
                case (System.Drawing.ContentAlignment) 3:
                    return VerticalAlignment.Top;
                default:
                    if (align != System.Drawing.ContentAlignment.MiddleLeft && align != System.Drawing.ContentAlignment.MiddleCenter)
                    {
                        return VerticalAlignment.Top;
                    }

                    break;
            }

            return VerticalAlignment.Center;
        }

        internal static Rectangle AlignInRectangle(Rectangle outer, Size inner, System.Drawing.ContentAlignment align)
        {
            int x = 0;
            int y = 0;
            if (align == System.Drawing.ContentAlignment.BottomLeft || align == System.Drawing.ContentAlignment.MiddleLeft || align == System.Drawing.ContentAlignment.TopLeft)
            {
                x = outer.X;
            }
            else if (align == System.Drawing.ContentAlignment.BottomCenter || align == System.Drawing.ContentAlignment.MiddleCenter || align == System.Drawing.ContentAlignment.TopCenter)
            {
                x = Math.Max(outer.X + (outer.Width - inner.Width) / 2, outer.Left);
            }
            else if (align == System.Drawing.ContentAlignment.BottomRight || align == System.Drawing.ContentAlignment.MiddleRight || align == System.Drawing.ContentAlignment.TopRight)
            {
                x = outer.Right - inner.Width;
            }

            if (align == System.Drawing.ContentAlignment.TopCenter || align == System.Drawing.ContentAlignment.TopLeft || align == System.Drawing.ContentAlignment.TopRight)
            {
                y = outer.Y;
            }
            else if (align == System.Drawing.ContentAlignment.MiddleCenter || align == System.Drawing.ContentAlignment.MiddleLeft || align == System.Drawing.ContentAlignment.MiddleRight)
            {
                y = outer.Y + (outer.Height - inner.Height) / 2;
            }
            else if (align == System.Drawing.ContentAlignment.BottomCenter || align == System.Drawing.ContentAlignment.BottomRight || align == System.Drawing.ContentAlignment.BottomLeft)
            {
                y = outer.Bottom - inner.Height;
            }

            return new Rectangle(x, y, Math.Min(inner.Width, outer.Width), Math.Min(inner.Height, outer.Height));
        }

        public void ShowContextMenuStrip()
        {
            if (this.skipNextOpen)
            {
                this.skipNextOpen = false;
                return;
            }

            this.State = PushButtonState.Pressed;
            if (this.m_SplitMenu != null)
            {
                this.m_SplitMenu.Show(this, new Point(0, base.Height));
                return;
            }

            if (this.m_SplitMenuStrip != null)
            {
                this.m_SplitMenuStrip.Show(this, new Point(0, base.Height), ToolStripDropDownDirection.BelowRight);
            }
        }

        private void SplitMenuStrip_Opening(object sender, CancelEventArgs e)
        {
            this.isSplitMenuVisible = true;
        }

        private void SplitMenuStrip_Closing(object sender, ToolStripDropDownClosingEventArgs e)
        {
            this.isSplitMenuVisible = false;
            this.SetButtonDrawState();
            if (e.CloseReason == ToolStripDropDownCloseReason.AppClicked)
            {
                this.skipNextOpen = (this.dropDownRectangle.Contains(base.PointToClient(Cursor.Position)) && Control.MouseButtons == MouseButtons.Left);
            }
        }

        private void SplitMenu_Popup(object sender, EventArgs e)
        {
            this.isSplitMenuVisible = true;
        }

        protected override void WndProc(ref Message m)
        {
            if (m.Msg == 530)
            {
                this.isSplitMenuVisible = false;
                this.SetButtonDrawState();
            }

            base.WndProc(ref m);
        }

        private void SetButtonDrawState()
        {
            if (base.Parent == null)
            {
                return;
            }

            Rectangle bounds = base.Bounds;
            if (base.Bounds.Contains(base.Parent.PointToClient(Cursor.Position)))
            {
                this.State = PushButtonState.Hot;
                return;
            }

            if (this.Focused)
            {
                this.State = PushButtonState.Default;
                return;
            }

            if (!base.Enabled)
            {
                this.State = PushButtonState.Disabled;
                return;
            }

            this.State = PushButtonState.Normal;
        }

        private const int SplitSectionWidth = 18;

        private PushButtonState _state;

        private static int BorderSize = SystemInformation.Border3DSize.Width * 2;

        private bool skipNextOpen;

        private Rectangle dropDownRectangle;

        private bool showSplit;

        private bool isSplitMenuVisible;

        private ContextMenuStrip m_SplitMenuStrip;

        private ContextMenu m_SplitMenu;

        private System.Windows.Forms.TextFormatFlags textFormatFlags;

        private bool isMouseEntered;
    }
}